import {
	getDateString
} from "@/assets/js/common.js"
let Controller = {
	mounted() {
		this.getDowmList();
	},
	activated(){
		this.requestOnSaleList();
	},
	methods: {
		getCard(){
			this.$ajax.get(this.apiTest + `/sale/saleOrder/statistics/5`)
			.then(res=>{
				const data = res.data;
				if(data.status == 200){
					const result = data.result;
					this.Model.configArr = result.map(item=>{
						return {
							color: item.color,
							label: item.tabName,
							value: item.tabIndex,
							count: item.total
						}
					})
				}
			}).catch(err=>{})
		},
		getDowmList() {
			/* 
			 type：1 售中
			 type: 2 售后
			 */
			this.$ajax.get(this.apiTest + `/sale/onSale/order/afterSale/orderBox/${2}`)
				.then(response => {
					const data = response.data;

					if (data.status == 200) {
						const result = data.result;
						/* 
						 saleOrderStatus订单状态[1]
						 saleOrderTypes售中类型-状态联动[2][3]
						 saleOrderChannels销售渠道[4][5]
						 deliveryTypes发货方式[6]
						 */
						this.Model.filterOptionData = result;
						this.Model.saleManList = {};
						this.Model.saleAfterOrderStatus = {};
						this.Model.filterOption.items[1].data = result.saleOrderStatus.map(item => {
							return {
								label: item.value,
								value: item.key
							}
						});
						this.Model.filterOption.items[2].data = result.saleOrderTypes.map((item, index) => {
							this.Model.saleAfterOrderStatus[item.key] = item.saleAfterOrderStatus;
							return {
								label: item.value,
								value: item.key
							}
						});
						this.Model.filterOption.items[4].data = result.saleOrderChannels.map((item, index) => {
							this.Model.saleManList[item.key] = item.saleManList;
							return {
								label: item.value,
								value: item.key
							}
						});
						this.Model.filterOption.items[6].data = result.deliveryTypes.map(item => {
							return {
								label: item.value,
								value: item.key
							}
						});
						this.Model.defaultFilterOption = _.cloneDeep(this.Model.filterOption)
					}
				}).catch(err => {
				})
		},
		requestOnSaleList() {
			const params = {
				condition: {
					...this.getSearchInfoParam(),
					tabIndex: 50
				},
				...this.Model.pager.params
			};
			this.$ajax
				.post(this.apiTest + `/sale/saleOrder/list`, params)
				// .post(apiTest + "/supplier/sapGoodsInfo/listDeliveryGoodsBill", params)
				.then(response => {
					const data = response.data;
					this.Model.tableData = data.result.result;
					this.Model.pager.pageTotal = data.result.total;
					this.Model.pager.pageIndex = data.result.pageIndex;
				})
				.then(res=>{
					this.getCard();
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		getSearchInfoParam() {
			let data = _.cloneDeep(this.Model.searchFromData);
			if (data.bookOrderTime) {
				data.bookOrderStartTime = getDateString(data.bookOrderTime[0]);
				data.bookOrderEndTime = getDateString(data.bookOrderTime[1]);
				data.bookOrderTime = undefined;
			}
			if (data.onSaleApplyTime) {
				data.onSaleApplyStartTime = getDateString(data.onSaleApplyTime[0]);
				data.onSaleApplyEndTime = getDateString(data.onSaleApplyTime[1]);
				data.onSaleApplyTime = undefined;
			}
			return data;
		},
		tableDetail(row) {
			let name;
			switch (row.afterSaleType) {
				/* （30售后升级换款 40常规售后 50售后退款） */
				//30,40跳变更,50跳退款
				case 30:
					name = 'afterOrderDetailChange';
					break;
				case 40:
					name = 'afterOrderDetailChange';
					break;
				case 50:
					name = 'afterOrderDetailRefund';
					break;
				default:
					break;
			}
			if (name) {
				this.$router.push({
					name: name,
					query: {
						data: JSON.stringify(row)
					}
				})
			}
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			if (val.length == 0) {
				this.Model.selectionList = [];
				return;
			};
			let arr = val.map((item, index, list) => {
				return item.id
			});
			this.Model.selectionList = arr;
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
			this.requestOnSaleList();
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
			this.requestOnSaleList();
		},
		cardClick(item) {
			this.toClear();
			this.Model.filterOption.items[2].value = item.value;
			// this.requestOnSaleList();
			// this.Model.cardSelected = color;
		},
		showChild(val, item, option) {
			/* 
			 saleOrderStatus订单状态[1]
			 saleOrderTypes售中类型-状态联动[2][3]
			 saleOrderChannels销售渠道[4][5]
			 deliveryTypes发货方式[6]
			 */
			for(let i = 0;i<7;i++){
				this.Model.filterOption.items[i] = option[i];
			}
			switch (item.key) {
				case 'saleOrderChannels':
					this.Model.filterOption.items[5].data = this.Model.saleManList[item.val].map(obj => {
						return {
							label: obj.value,
							value: obj.key
						}
					});
					this.Model.filterOption.items[5].value = undefined;
					break;
				case 'saleOrderTypes':
					this.Model.filterOption.items[3].data = this.Model.saleAfterOrderStatus[item.val].map(obj => {
						return {
							label: obj.value,
							value: obj.key
						}
					});
					this.Model.filterOption.items[3].value = undefined;
					break;
				default:
					break;
			};
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			this.requestOnSaleList();
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
		},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		getStatusText(type, value) {
			let text = '';
			switch (type) {
				case 'payStatus':
					/* 0未支付/1部分支付/2已支付 */
					switch (value) {
						case 0:
							text = '未支付';
							break;
						case 1:
							text = '部分支付';
							break;
						case 2:
							text = '已支付';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'orderStatus':
					/* 0待付款/1待审核/2待发货/3待提货/4部分已发货/5待收货/6已完成/7已关闭 */
					switch (value) {
						case 0:
							text = '待付款';
							break;
						case 1:
							text = '待审核';
							break;
						case 2:
							text = '待发货';
							break;
						case 3:
							text = '待提货';
							break;
						case 4:
							text = '部分已发货';
							break;
						case 5:
							text = '待收货';
							break;
						case 6:
							text = '已完成';
							break;
						case 7:
							text = '已关闭';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'aftersaleStatus':
					/* 售后：0退款中/1退款成功/2退款拒绝/ 售中：3待审核/4已取消/5变更成功/6待用户支付/7待平台退款 */
					switch (value) {
						case 0:
							text = '退款中';
							break;
						case 1:
							text = '退款成功';
							break;
						case 2:
							text = '退款拒绝';
							break;
						case 3:
							text = '待审核';
							break;
						case 4:
							text = '已取消';
							break;
						case 5:
							text = '变更成功';
							break;
						case 6:
							text = '待用户支付';
							break;
						case 7:
							text = '待平台退款';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'orderType':
					/* 10现货订单/20定制订单/30售中订单/40售后订单 */
					switch (value) {
						case 10:
							text = '现货订单';
							break;
						case 20:
							text = '定制订单';
							break;
						case 30:
							text = '售中订单';
							break;
						case 40:
							text = '售后订单';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				case 'afterSaleType':
					/* （10售中变更 20退款 30升级换款 40常规售后） */
					switch (value) {
						case 10:
							text = '售中变更';
							break;
						case 20:
							text = '退款';
							break;
						case 30:
							text = '升级换款';
							break;
						case 40:
							text = '常规售后';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				default:
					break;
			}
			return text;
		},
		approval(row) {
			const params = row ? [row] : this.Model.selectionList;
			if (params && params.length > 0) {
				this.$confirm('是否确认审核?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					this.$ajax
						.post(this.apiTest + `/sale/onSale/order/afterSale/approval`, params)
						.then(response => {
							const data = response.data;
							if (data.status == 200 && data.result == 1) {
								this.$message({
									type: 'success',
									message: '已成功审核'
								});
								this.requestOnSaleList();
							} else {
								this.$message({
									type: 'error',
									message: data.msg
								});
							}
						})
						.catch(error => {
							this.$message.error('请求错误');
						});
				}).catch(() => {

				});
			}
		},
		cancelAfterSale(id) {
			this.$confirm('是否确认取消售后?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax
					.get(this.apiTest + `/sale/onSale/order/sale/cancel/${id}`)
					.then(response => {
						const data = response.data;
						if (data.status == 200 && data.result == 1) {
							this.$message({
								type: 'success',
								message: '已成功'
							});
							this.requestOnSaleList();
						}
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}).catch(() => {

			});

		},
		showPayDialog(id) {
			this.Model.dialogFormVisible = true;
			this.Model.nowSelectId = id;
		},
		pay() {
			const form = this.Model.dialogForm;
			const params = {
				payMethod: form.payMethod,
				paymentMoney: form.paymentMoney,
				serialNumber: form.serialNumber,
				payTime: getDateString(form.payTime),
				orderId: this.Model.nowSelectId
			};
			this.$ajax
				.post(this.apiTest + `/sale/onSale/order/afterSale/pay`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200 && data.result == 1) {
						this.$message({
							type: 'success',
							message: '已成功'
						});
						this.requestOnSaleList();
					};
					this.Model.dialogFormVisible = false;
				})
				.catch(error => {
					this.$message.error('请求错误');
					this.Model.dialogFormVisible = false;
				});
		},
	}
}
export default Controller;
